﻿using Quartz;
using System;
using System.Threading;
using System.Threading.Tasks;

namespace AVCJobDispatcherWindows.TaskJobEngine
{
    public class JobListener : IJobListener
    {
        public string Name => "JobListener";

        /// <summary>
        // 这个方法正常情况下不执行,但是如果当TriggerListener中的vetoJobExecution方法返回true时,那么执行这个方法.
        /// </summary>
        /// <param name="context"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task JobExecutionVetoed(IJobExecutionContext context, CancellationToken cancellationToken = default(CancellationToken))
        {
            return Task.CompletedTask;
        }

        /// <summary>
        /// 执行前执行
        /// </summary>
        /// <param name="context"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task JobToBeExecuted(IJobExecutionContext context, CancellationToken cancellationToken = default(CancellationToken))
        {
            return Task.CompletedTask;
        }

        /// <summary>
        /// 执行任务后
        /// </summary>
        /// <param name="context"></param>
        /// <param name="jobException"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task JobWasExecuted(IJobExecutionContext context, JobExecutionException jobException, CancellationToken cancellationToken = default(CancellationToken))
        {
            return Task.CompletedTask;
        }

        DateTimeOffset? ConvertTime(DateTimeOffset? timr)
        {
            if (timr != null)
            {
                DateTimeOffset dtoff = (DateTimeOffset)timr;
                return dtoff.ToLocalTime();
            }
            return null;
        }
    }
}
